﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace sand_game_core
{
    public class Water : particle
    {
        public Water(uint x, uint y, int startLight)
            : base(new Microsoft.Xna.Framework.Color(0 + random.rnd.Next(60), 50 + random.rnd.Next(60), 100 + random.rnd.Next(60), 130),startLight)
        {
            base.x = x;
            base.y = y;
        }
        public override void update()
        {
                downParticle = y >= 1 ? world.worldParticleLayer[x * world.arraywidth + y - 1] : NewParticle.blank;
            ammount = (uint)random.rnd.Next(3);
            if (downParticle == null)
            {
                world.swap(x, y, x, y - 1);
            }
            if (ammount > 0)
            {
                    RightParticle = x >= ammount ? world.worldParticleLayer[(x - ammount) * world.arraywidth + y] : null;
                    LeftParticle = world.arraywidth > y + ammount ? world.worldParticleLayer[(x + ammount) * world.arraywidth + y] : null;

                if ((RightParticle is Sand || RightParticle == null) && !(LeftParticle is Sand || LeftParticle == null))
                {
                    world.swap(x, y, x - ammount, y);
                }
                else if ((LeftParticle is Sand || LeftParticle == null) && !(RightParticle is Sand || RightParticle == null))
                {
                    world.swap(x, y, x + ammount, y);
                }
                else
                {
                    if (random.rnd.Next(2) == 0)
                    {
                        world.swap(x, y, x + ammount, y);
                    }
                    else
                    {
                        world.swap(x, y, x - ammount, y);
                    }
                }
            }
            else if (LeftParticle is Lava)
            {

                LeftParticle.toTransition = new Obsidian(LeftParticle.x, LeftParticle.y, light);
                this.toTransition = new Obsidian(x, y, light);
            }
            else if (RightParticle is Lava)
            {
                RightParticle.toTransition = new Obsidian(RightParticle.x, RightParticle.y, light);
                this.toTransition = new Obsidian(x, y, light);
            }
            else if (downParticle is Lava)
            {
                downParticle.toTransition = new Obsidian(downParticle.x, downParticle.y, light);
                this.toTransition = new Obsidian(x, y, light);
            }
            else
            {
                active = false;
            }
        }
        public override void lightMe()
        {
                light -= light >= 1 ? 1 : 0;
        }
        public override void reactivate()
        {
                active = (x + 1 < world.arraywidth && y + 1 < world.arrayheight && x - 1 < world.arraywidth && y - 1 < world.arrayheight && (world.worldParticleLayer[(x + 1) * world.arraywidth + y] == null || world.worldParticleLayer[(x - 1) * world.arraywidth + y] == null || world.worldParticleLayer[x * world.arraywidth + y + 1] == null || world.worldParticleLayer[x * world.arraywidth + y - 1] == null)) ? true : false;
        }
    }
}